﻿using AutoMapper;
using NetTaste;
 
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using VM.Common.Result;
using VM.Entity.EntityDto.ShopCar;
using VM.Entity.EntityMap.Goods;
using VM.Entity.EntityMap.ICon;
using VM.Entity.EntityMap.Lunbo;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.ShopCar;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IDAL.ShopCar;

namespace VM.DAL.ShopCar
{
    public class ShopCarDAL : BaseDAL, IShopCarDAL
    {

        protected ISqlSugarClient _Client { get; set; }
        protected IMapper _IMapper { get; set; }

        public ShopCarDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper = iMapper;
        }
 

        public bool addShopCar(sys_shopCar obj,int addTypeId)
        {
            //判断是否存在
            //如果已存在，则更新，否则添加
              //直接在购物车修改
            if (addTypeId == 2)
            {   
                    _Client.Updateable(obj).UpdateColumns(it => new { it.qty })
                      .Where(a => a.createUserId == obj.createUserId)
                      .Where(a => a.goodId == obj.goodId)
                       .Where(a => a.specId == obj.specId)
                        .ExecuteCommand(); ;
                
                return true;
            }
            else  //由详情页来
            {
                //判断购物车内是否存在
                List<sys_shopCar> listCheck = _Client.Queryable<sys_shopCar>()
                                                  .Where(a => a.createUserId == obj.createUserId)
                                                   .Where(a => a.goodId == obj.goodId)
                                                    .Where(a => a.specId == obj.specId)
                                                    .ToList();
                if (listCheck.Count > 0)
                {
                    for (int i = 0; i < listCheck.Count; i++)
                    {
                        sys_shopCar item = listCheck[i];
                        _Client.Updateable(obj)
                   .ReSetValue(it => { it.qty = it.qty + item.qty; })
                    .UpdateColumns(it => new { it.qty })
                     .Where(a => a.createUserId == item.createUserId)
                      .Where(a => a.goodId == item.goodId)
                      .Where(a => a.specId == item.specId)
                        .ExecuteCommand(); ;
                    }
                    return  true;
                }
                else
                {
                    int Res = _Client.Insertable(obj)
                              .ExecuteCommand();
                    if (Res > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            } 
        }
 
        /// <summary>
        /// 获取商品列表，根据商品类别
        /// </summary>
        /// <param name="goodsTypeId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PagingData<getShopCarListDto> getShopCarList(int userId, int pageIndex, int pageSize)
        {
            var listRes =
                 _Client.Queryable<sys_shopCar>()//.ToList();
                .Includes(a => a.good)
                 .Includes(a=>a.good.headImgs)
                .Includes(a => a.spec)
                .Includes(a => a.createUser)
                .Where(a => a.createUserId == userId)
                .OrderByDescending(a => a.id)
                ;

            PagingData<sys_shopCar> result = new PagingData<sys_shopCar>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
                searchString = ""
            };
            PagingData<getShopCarListDto> resend = _IMapper.Map<PagingData<sys_shopCar>, PagingData<getShopCarListDto>>(result);

            return resend;
        }


        public bool deleteShopCarById(int userId, int shopCarId)
        {
            int  listRes =
                  _Client.Deleteable<sys_shopCar>()//.ToList();
                 .Where(a => a.id == shopCarId).ExecuteCommand() ;
            if (listRes == 1)
            {
                return true;
            }
            else
            {

                return false;
            }
        }
    }
}
